/**
 * JungoMobileWeb
 * DealServiceImpl.java
 * 
 * Copyright 2013 Teachizen
 * Written by cloudstick
 *	2013. 8. 11. first written.
 *
 * note:
 */
package com.jungo.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jungo.web.bean.Deal;
import com.jungo.web.bean.Favorite;
import com.jungo.web.bean.Product;
import com.jungo.web.constant.DealStatus;
import com.jungo.web.dao.DealDao;
import com.jungo.web.dao.ProductDao;
import com.jungo.web.service.DealService;

/**
 * @author cloudstick
 *
 */
@Service
@Slf4j
public class DealServiceImpl implements DealService {

	@Autowired
	private DealDao dealDao;
	
	@Autowired
	private ProductDao productDao;
	
	/* (non-Javadoc)
	 * @see com.jungo.web.service.DealService#deal(com.jungo.web.bean.Favorite)
	 */
	@Override
	public List<Product> deal(Favorite favorite) {
		//search product by price, category
		//product status(21:confirm)
		
		List<Product> products = dealDao.getProductByFavorite(favorite);
		if ( products == null || products.isEmpty() ) {
			log.debug("Have no product by favorite[{}]", favorite);
			return null;
		}
		
		List<Product> registeredProducts = new ArrayList<Product>();
		Deal deal = null;
		for ( Product product : products ) {
			log.debug("Matched product[{}]", product);
			
			deal = new Deal();
			deal.setPid(product.getPid());
			deal.setFavorite_userid(favorite.getUserid());
			deal.setDid(dealDao.getDealId());
			deal.setProduct_userid(product.getUserid());
			deal.setFid(favorite.getFid());
			deal.setStatus(DealStatus.DEALING.getValue());
			
			dealDao.updateFavoriteCount(product.getPid());
			dealDao.setDeal(deal);
			updateProductStatus(product);
			registeredProducts.add(product);
		}
		favorite.setStatus(11);
		productDao.updateFavoriteStatus(favorite);
		return registeredProducts;
	}

	/* (non-Javadoc)
	 * @see com.jungo.web.service.DealService#deal(com.jungo.web.bean.Product)
	 */
	@Override
	public void deal(Product product) {
		Favorite favorite = dealDao.getFavoriteByProduct(product);
		if ( favorite == null ) {
			log.debug("Have no matched favorite. product[{}]", product);
			return;
		}
		log.info("Matched favorite[{}]", favorite);
		
		Deal deal = new Deal();
		int did = dealDao.getDealId();
		deal.setDid(did);
		deal.setFavorite_userid(favorite.getUserid());
		deal.setFid(favorite.getFid());
		deal.setPid(product.getPid());
		deal.setProduct_userid(product.getUserid());
		deal.setStatus(DealStatus.DEALING.getValue());
		dealDao.setDeal(deal);
		
		updateProductStatus(product);
		favorite.setStatus(11);
		productDao.updateFavoriteStatus(favorite);
		
		dealDao.updateFavoriteCount(product.getPid());
	}

	/* (non-Javadoc)
	 * @see com.jungo.web.service.DealService#dealWithOne(com.jungo.web.bean.Favorite)
	 */
	@Override
	public boolean dealWithOne(Favorite favorite) {
		List<Product> products = dealDao.getProductByFavorite(favorite);
		if ( products == null || products.isEmpty() ) {
			log.debug("Have no product by favorite[{}]", favorite);
			return true;
		}
		
		if ( products.size() != 1 ) {
			throw new RuntimeException("Invalid operation.");
		}
		
		Product product = products.get(0);
		
		Deal deal = new Deal();
		deal.setPid(product.getPid());
		deal.setFavorite_userid(favorite.getUserid());
		if ( dealDao.checkDuplicateDeal(deal) == false ) {
			throw new RuntimeException("Invalid operation.");
		}
		
		deal.setDid(dealDao.getDealId());
		deal.setProduct_userid(product.getUserid());
		deal.setFid(favorite.getFid());
		deal.setStatus(DealStatus.DEALING.getValue());
		
		dealDao.setDeal(deal);
		dealDao.updateFavoriteCount(product.getPid());
		
		updateProductStatus(product);
		favorite.setStatus(11);
		productDao.updateFavoriteStatus(favorite);
		return true;
	}
	
	private void updateProductStatus(Product product) {
		product.setStatus(31);
		productDao.updateProductStatus(product);
	}

}
